$(function() {
                

                
                

               var imgshelfid= configBird("droppic","imgshelf","",300,300,'imgshelf',false,false,"upThumImg");
               $("#shelfBtn").click(function() {
               
                $("#filebirdimage-"+imgshelfid).trigger('click');

                
                });
                
                
                var imgshelfid2= configBird("droppic2","imgshelf2","",300,300,'imgshelf2',false,false,"upThumImg");
               $("#shelfBtn2").click(function() {
               
                $("#filebirdimage-"+imgshelfid2).trigger('click');

                
                });
//               
//               var imgbanner1= configBird("droppic1","imgbanner1","",960,460,'imgbanner1',false,false,"upScsImgA");
//               $("#bannerBtn1").click(function() {
//               
//                $("#filebirdimage-"+imgbanner1).trigger('click');

//                
//                });
//                
//            
//             var imgbanner2= configBird("droppic2","imgbanner2","",960,460,'imgbanner2',false,false,"upScsImgA");
//               $("#bannerBtn2").click(function() {
//               
//                $("#filebirdimage-"+imgbanner2).trigger('click');

//                
//                });
//                
//             var imgbanner3= configBird("droppic3","imgbanner3","",960,460,'imgbanner3',false,false,"upScsImgA");
//               $("#bannerBtn3").click(function() {
//               
//                $("#filebirdimage-"+imgbanner3).trigger('click');

//                
//                });
//                
//             var imgbanner4= configBird("droppic4","imgbanner4","",960,460,'imgbanner4',false,false,"upScsImgA");
//             
//             $("#bannerBtn4").click(function() {
//               
//                $("#filebirdimage-"+imgbanner4).trigger('click');

//                
//                });
//                
//             var imgbanner5= configBird("droppic5","imgbanner5","",960,460,'imgbanner5',false,false,"upScsImgA");
//             
//             $("#bannerBtn5").click(function() {
//               
//                $("#filebirdimage-"+imgbanner5).trigger('click');
//                
//                
//                });
//             
//             configBirdFile("wrapPEM",['pem'],'pemfile','application/pam','btnAddfile5');   
//             
                
             $("#userAddBtn").click(function() {
               
                 // var usernameArr=$("input[name='username[]']").serializeArray();
                  var usernameArr = [];    
                  $("input[name='username[]']").each(function(i, el){
                  usernameArr.push( $(el).val() );
                  });
                  
                  
                  var emailArr= [];
                  
                   $("input[name='email[]']").each(function(i, el){
                  emailArr.push( $(el).val() );
                  });
                 // console.log(usernameArr);
                  
                   if($("#usernameadd").val()=="")
                   {
                      alert("โปรดกรอก Username");
                   }else if(!validateEmail($("#emailadd").val()))
                   {
                      alert("ไม่ถูกรูปแบบ Email");
                   }else if($("#passwordadd").val()=="")
                   {
                      alert("โปรดกรอก Password");
                   }else
                   {
                    postCORS(serviceinfo+'validateuser',{username:$("#usernameadd").val(),password:$("#passwordadd").val(),email:$("#emailadd").val()} ,function(data) { 
                    if(data.error)
                    {
                    alert(data.error);
                    
                    }else
                    {
                     if(usernameArr.indexOf($("#usernameadd").val())!=-1)
                     {
                     
                      alert("Username ซ้ำ");
                     }else if(emailArr.indexOf($("#emailadd").val())!=-1)
                     {
                     
                      alert("Email ซ้ำ");
                     }else
                     {
                     
                      addUser(); 
                     }
                     
                    
                    
                     
                    
                    }
                    
                    });
                   
                   }

                
                });
                
           $("#addType").click(function() {
             var catnameArr= [];
             if($("#catnameadd").val())
             {
             $("input[name='category[]']").each(function(i, el){
                  catnameArr.push( $(el).val() );
                  });
                  
              if(catnameArr.indexOf($("#catnameadd").val())!=-1)
                     {
                     
                      alert("Type ซ้ำ");
                     }else
                     {
                     
                      addCat(); 
                     }
                     
             }else
             {
             
                   alert("โปรดกรอก Type");
             }      
            
             
             });
             
           $("#addTag").click(function() {
             var tagArr= [];
             if($("#tagnameadd").val())
             {
             $("input[name='tag[]']").each(function(i, el){
                  tagArr.push( $(el).val() );
                  });
                  
              if(tagArr.indexOf($("#tagnameadd").val())!=-1)
                     {
                     
                      alert("Tag ซ้ำ");
                     }else
                     {
                     
                      addTag(); 
                     }      
            
             }else
             {
             
                   alert("โปรดกรอก Tag");
             }      
             });
                
        
                
                //left value = img width + margin-right
                $(".dotOut").click(function() {
                    $(".dotOut").addClass("dotIn").not(this).removeClass("dotIn");
                    switch (true) {
                      case $(this).hasClass('dotP1'):
                            $(".wrapTumbnlDef").animate({
                                left: '0'
                            }, 700 );
                            break;
                      case $(this).hasClass('dotP2'):
                            $(".wrapTumbnlDef").animate({
                                left: '-805px'
                            }, 700 );
                            break;
                      case $(this).hasClass('dotP3'):
                            $(".wrapTumbnlDef").animate({
                                left: '-1610px'
                            }, 700 );
                            break;
                      case $(this).hasClass('dotP4'):
                            $(".wrapTumbnlDef").animate({
                                left: '-2415px'
                            }, 700 );
                            break;
                    }
                 });
            
            
            });
 var idrow=1;
 var deleterowuserid=[];     
function removerow(idrow2)
{    

 

      if(confirm('ต้องการลบรายชื่อนี้ออกจากระบบใช่หรือไม่'))
      {
      if($("#useridrow-"+idrow2).val())
       {
       deleterowuserid.push( $("#useridrow-"+idrow2).val() );
       }
       
       var anSelected =oTable.$('tr.rowdata'+idrow2);
       if ( anSelected.length !== 0 ) {
            oTable.fnDeleteRow( anSelected[0] );
        }
       
      
      }
      
      //console.log(deleterowuserid);

}


function addUser()
{
var code = '<tr id="rowdata'+idrow+'" class="rowdata'+idrow+'">\n';
code += '<td>'+idrow+'</td>\n';
code += '<td>\n';
code += '<input class="inptNameUser inptUserOld" type="text" placeholder="maruko" name="username[]" value="'+$("#usernameadd").val()+'">\n';
code += '</td>\n';

code += '<td>\n';
code += '<input class="inptEmailUser inptUserOld" type="text" name="email[]" placeholder="maruko@live.com" value="'+$("#emailadd").val()+'">\n';
code += '</td>\n';



code += '<td>\n';
code += '<input class="inptNameUser inptUserOld" type="text" placeholder="maruko" name="firstname[]" value="'+$("#firstnameadd").val()+'">\n';
code += '</td>\n';

code += '<td>\n';
code += '<button class="btnResetPwd btnCreateS" type="button">reset</button>\n';
code += '</td>\n';

code += '<td>\n';
code += '<input  type="checkbox" name="role[]"  value="1">\n';
code += '</td>\n';


code += '<td>\n';
code += '<i class="iconDelete" onclick="removerow('+idrow+')"></i>\n';
code += '<input type="hidden" name="password[]" value="'+$("#passwordadd").val()+'">\n';
code += '<input type="hidden" name="userid[]" id="useridrow-'+idrow+'" value="">\n';
code += '<input type="hidden" name="photoavatar[]"  value="'+$("#filebirdsavename-1").val()+'">\n';
code += '</td>\n';
code += '</tr>';
var code2="";
code2 += '<i class="iconDelete" onclick="removerow('+idrow+')"></i>\n';
code2 += '<input type="hidden" name="password[]" value="'+$("#passwordadd").val()+'">\n';
code2 += '<input type="hidden" name="userid[]" id="useridrow-'+idrow+'" value="">\n';
code2 += '<input type="hidden" name="photoavatar[]"  value="'+$("#filebirdsavename-1").val()+'">\n';


$('#wrapTableUser').dataTable().fnAddData( [
        idrow,
        '<input class="inptNameUser inptUserOld" type="text" placeholder="maruko" name="username[]" value="'+$("#usernameadd").val()+'">',
        '<input class="inptEmailUser inptUserOld" type="text" name="email[]" placeholder="maruko@live.com" value="'+$("#emailadd").val()+'">\n',
        '<input class="inptEmailUser inptUserOld" type="text" name="firstname[]" placeholder="maruko" value="'+$("#firstnameadd").val()+'">\n',
        '<button class="btnResetPwd btnCreateS" type="button">reset</button>\n',
    
        '<input  type="checkbox" name="role[]"  value="1">\n',
        '<button class="btnResetPwd btnCreateS" type="button">Edit</button>\n',
        code2 ] );

//$('#wrapTableUser tbody').append(code);
idrow++;
$("#usernameadd").val("");
$("#emailadd").val("");
$("#passwordadd").val("");
$("#firstnameadd").val("");
$("#filebirdsavename-1").val("");
$("#imgshelf2").attr("src",'img/thumnailshefdefault.jpg');

}
function addUserByEdit(userid,username,email,roleid,firstname,photoavatar)
{
var code = '<tr id="rowdata'+idrow+'" class="rowdata'+idrow+'">\n';
code += '<td>'+idrow+'</td>\n';
code += '<td>\n';
code += '<input class="inptNameUser inptUserOld" type="text" placeholder="maruko" name="username[]" value="'+username+'">\n';
code += '</td>\n';

code += '<td>\n';
code += '<input class="inptEmailUser inptUserOld" type="text" name="email[]" placeholder="maruko@live.com" value="'+email+'">\n';
code += '</td>\n';


code += '<td>\n';
code += '<input class="inptNameUser inptUserOld" type="text" name="firstname[]" placeholder="" value="'+firstname+'">\n';

code += '</td>\n';


code += '<td>\n';
code += '<button class="btnResetPwd btnCreateS" onclick="sendEmail(\''+email+'\')" type="button">reset</button>\n';
code += '</td>\n';

code += '<td>\n';

var checked='';
if(roleid==1)
{
    checked='checked="checked"';
}
code += '<input '+checked+'  type="checkbox" name="role[]"  value="1">\n';
code += '</td>\n';


code += '<td>\n';
code += '<button class="btnResetPwd btnCreateS" onclick="location.href=\'usersettingdata?userid='+userid+'\'" type="button">Edit</button>\n';
code += '</td>\n';

code += '<td>\n';
code += '<i class="iconDelete" onclick="removerow('+idrow+')"></i>\n';
code += '<input type="hidden" name="password[]" value="">\n';
code += '<input type="hidden" name="userid[]" id="useridrow-'+idrow+'" value="'+userid+'">\n';
code += '<input  type="hidden" name="photoavatar[]"  value="'+photoavatar+'">\n';
code += '</td>\n';
code += '</tr>';

$('#wrapTableUser tbody').append(code);
idrow++;

}



var idrowcat=1;
var deleterowcatid=[];     
function removerowcat(idrow2)
{     if($("#catidrow-"+idrow2).val())
       {
       deleterowcatid.push( $("#catidrow-"+idrow2).val() );
       }
      $('#removerowcat'+idrow2).remove();
      
      //console.log(deleterowuserid);

}
function addCat()
{
//var code = '<tr id="removerowcat'+idrowcat+'">\n';
//code += '<td>'+idrowcat+'</td>\n';
//code += '<td>\n';
//code += '<input class="inptNameUser inptUserOld" type="text" placeholder="Category" name="category[]" value="'+$("#catnameadd").val()+'">\n';
//code += '</td>\n';
//code += '<td>\n';
//code += '<i class="iconDelete" onclick="removerowcat('+idrowcat+')"></i>\n';
//code += '<input type="hidden" name="catid[]" id="catidrow-'+idrowcat+'" value="">\n';
//code += '</td>\n';
//code += '</tr>';

//$('#wrapTableCat tbody').append(code);
//idrowcat++;
//$("#catnameadd").val("");
var code = '<div id="removerowcat'+idrowcat+'" style="display: inline-block;"  class="wrpTypeShow">\n';
code += '<button class="deleteType" onclick="removerowcat('+idrowcat+')">x</button>\n';
code += $("#catnameadd").val();
code += '<input type="hidden" name="category[]"  value="'+$("#catnameadd").val()+'">\n'; 
code += '<input type="hidden" name="catid[]" id="catidrow-'+idrowcat+'" value="">\n'; 
code += '</div>';
$('#typeWrpShowCat').append(code); 
$("#catnameadd").val("");
idrowcat++;

}
function resetbannerlibrary(rowdataset)
{
    var code = '<img alt="upimage" src="img/banner.png" class="upUIUA" id="imgbanner'+rowdataset+'"><div class="upUIUA">\n';
code += '<button type="button" id="bannerBtn'+rowdataset+'" class="btnAddUIU btnAddU">+ Store Banner</button>\n';
code += '</div>';
$("#filebirdsavename-"+rowdataset).val('');
$("#droppic"+rowdataset).html(code);
$("#bannerLink-"+rowdataset).val('');

$("#bannerBtn"+rowdataset).click(function() {
               
                $("#filebirdimage-"+rowdataset).trigger('click');

                
                });

}
function resetbanner(rowdataset)
{
var code = '<img alt="upimage" src="img/banner.png" class="upUIUA" id="imgbanner'+rowdataset+'"><div class="upUIUA">\n';
code += '<button type="button" id="bannerBtn'+rowdataset+'" class="btnAddUIU btnAddU">+ Store Banner</button>\n';
code += '</div>';
$("#filebirdsavename-"+rowdataset).val('');
$("#droppic"+rowdataset).html(code);
$("#bannerLink-"+rowdataset).val('');

$("#bannerBtn"+rowdataset).click(function() {
               
                $("#filebirdimage-"+rowdataset).trigger('click');

                
                });

}
function addCatByEdit(catid,name)
{
/*var code = '<tr id="removerowcat'+idrowcat+'">\n';
code += '<td>'+catid+'</td>\n';
code += '<td>\n';
code += '<input class="inptNameUser inptUserOld" type="text" placeholder="Category" name="category[]" value="'+name+'">\n';
code += '</td>\n';
code += '<td>\n';
code += '<i class="iconDelete" onclick="removerowcat('+idrowcat+')"></i>\n';

code += '<input type="hidden" name="catid[]" id="catidrow-'+idrowcat+'" value="'+catid+'">\n';
code += '</td>\n';
code += '</tr>';

$('#wrapTableCat tbody').append(code);
idrowcat++; */

var code = '<div id="removerowcat'+idrowcat+'" style="display: inline-block;"  class="wrpTypeShow">\n';
code += '<button class="deleteType" onclick="removerowcat('+idrowcat+')">x</button>\n';
code += name;
code += '<input type="hidden" name="category[]"  value="'+name+'">\n';  
code += '<input type="hidden" name="catid[]" id="catidrow-'+idrowcat+'" value="'+catid+'">\n'; 
code += '</div>';
$('#typeWrpShowCat').append(code); 

idrowcat++;

}


var idrowtag=1;
var deleterowtagid=[];     
function removerowtag(idrow2)
{     if($("#tagidrow-"+idrow2).val())
       {
       deleterowtagid.push( $("#tagidrow-"+idrow2).val() );
       }
      $('#removerowtag'+idrow2).remove();
      
      //console.log(deleterowuserid);

}
function addTag()
{
//var code = '<tr id="removerowtag'+idrowtag+'">\n';
//code += '<td>'+idrowtag+'</td>\n';
//code += '<td>\n';
//code += '<input class="inptNameUser inptUserOld" type="text" placeholder="Tag" name="tag[]" value="'+$("#tagnameadd").val()+'">\n';
//code += '</td>\n';
//code += '<td>\n';
//code += '<i class="iconDelete" onclick="removerowtag('+idrowtag+')"></i>\n';
//code += '<input type="hidden" name="tagid[]" id="tagidrow-'+idrowtag+'" value="">\n';
//code += '</td>\n';
//code += '</tr>';

//$('#wrapTableTag tbody').append(code);
//idrowtag++;
//$("#tagnameadd").val("");

var code = '<div id="removerowtag'+idrowtag+'"  >\n';


code += '<input type="text"  name="tag[]"  value="'+ $("#tagnameadd").val()+'">\n';

code += '<button  onclick="removerowtag('+idrowtag+')">x</button>\n';

code += '<input type="hidden" name="tagid[]" id="tagidrow-'+idrowtag+'" value="">\n'; 
code += '</div>';
$('#tagWrpShowTag').append(code); 
$("#tagnameadd").val("");
idrowtag++;

}
function addTagByEdit(tagid,name)
{
//var code = '<tr id="removerowtag'+idrowtag+'">\n';
//code += '<td>'+tagid+'</td>\n';
//code += '<td>\n';
//code += '<input class="inptNameUser inptUserOld" type="text" placeholder="Tag" name="tag[]" value="'+name+'">\n';
//code += '</td>\n';
//code += '<td>\n';
//code += '<i class="iconDelete" onclick="removerowtag('+idrowtag+')"></i>\n';

//code += '<input type="hidden" name="tagid[]" id="tagidrow-'+idrowtag+'" value="'+tagid+'">\n';
//code += '</td>\n';
//code += '</tr>';

//$('#wrapTableTag tbody').append(code);
//idrowtag++;

var code = '<div id="removerowtag'+idrowtag+'"  >\n';


code += '<input  type="text" name="tag[]"  value="'+ name+'">\n';


code += '<button  onclick="removerowtag('+idrowtag+')">x</button>\n';

code += '<input type="hidden" name="tagid[]" id="tagidrow-'+idrowtag+'" value="'+tagid+'">\n'; 
code += '</div>';
$('#tagWrpShowTag').append(code); 

idrowtag++;



}


function sendEmail(email)
{
if(confirm('Reset Password ?'))
{
postCORS(serviceinfo+'forgotpassword',{email:email} ,function(data) {
 if(data.error==0)
                  {
                    alert("โปรดเช็คเมล์");
                  
                  }else
                  {
                  
                   alert(data.error);
                   
                  }
});
}

}


function removeshelf()
{
                    if($("#shelfid").val())
                    {
                    
                    if(confirm("Remove ?"))
                    {
                      loadsave();
                      postCORS(serviceinfo+'removeshelf',{
                      
                         shelfid:$("#shelfid").val()
                      
                      
                      } ,function(data) {
                      loadclosesave();
                      if(data.error)
                      {
                      
                      alert(data.error);
                      }else
                      {
                     location.href=baseinfo+"/portalshelf";  
                      
                      }
                      });
                    
                    }
                    }

}

function exportshelf()
{
                    if($("#shelfid").val())
                    {
                    if(confirm("Export ?"))
                    {
                      loadsave();
                      postCORS(serviceinfo+'exportshelf',{
                      
                         shelfid:$("#shelfid").val()
                      
                      
                      } ,function(data) {
                      loadclosesave();
                      if(data.error)
                      {
                      
                      alert(data.error);
                      }else
                      {
                     location.href=zipinfo+"/"+data.zip;  
                      
                      }
                      });
                    
                    }
                    }

}


function saveshelfbook()
{


    if($("#filebirdsavename-0").val()=="")
    {
      alert("เลือกรูป Thubmnail");
    
    }else if($("#title").val()=="")
    {
     alert("กรอก Title");
    
    }else if($("#descritpion").val()=="")
    {
     alert("กรอก Description");
    
    }else if($("#filebirdsavename-1").val()==""&&$("#filebirdsavename-2").val()==""&&$("#filebirdsavename-3").val()==""&&$("#filebirdsavename-4").val()=="")
    {
     alert("เลือกรูป Banner อย่างน้อย 1 รูป");
    
    }else if($("#filebirdsavename-1").val()&&$("#bannerLink-1").val()=="")
    {
     alert("กรุณากรอก Link1 ที่ Banner 1");
    
    }else if($("#filebirdsavename-2").val()&&$("#bannerLink-2").val()=="")
    {
     alert("กรุณากรอก Link2 ที่ Banner 2");
    
    }else if($("#filebirdsavename-3").val()&&$("#bannerLink-3").val()=="")
    {
     alert("กรุณากรอก Link3 ที่ Banner 3");
    
    }else if($("#filebirdsavename-4").val()&&$("#bannerLink-4").val()=="")
    {
     alert("กรุณากรอก Link4 ที่ Banner 4");
    
    }else if($("#filebirdsavename-5").val()&&$("#bannerLink-5").val()=="")
    {
     alert("กรุณากรอก Link5 ที่ Banner Library");
    
    }else
    {
                 var usernameArr = [];    
                  oTable.$("input[name='username[]']").each(function(i, el){
                  usernameArr.push( $(el).val() );
                  });
                  
                  
                  var firstnameArr = [];    
                  oTable.$("input[name='firstname[]']").each(function(i, el){
                  firstnameArr.push( $(el).val() );
                  });
                  
                  
                  var emailArr= [];
                  oTable.$("input[name='email[]']").each(function(i, el){
                  emailArr.push( $(el).val() );
                  });
                  
                  var password= [];
                  oTable.$("input[name='password[]']").each(function(i, el){
                  password.push( $(el).val() );
                  });
                  
                  var userid= [];
                  oTable.$("input[name='userid[]']").each(function(i, el){
                  userid.push( $(el).val() );
                  });
                  
                  var category= [];
                  $("input[name='category[]']").each(function(i, el){
                   category.push( $(el).val() );
                  });
                  
                  var catid= [];
                  $("input[name='catid[]']").each(function(i, el){
                  catid.push( $(el).val() );
                  });
                  
                  
                   var photoAvatarArr= [];
                  oTable.$("input[name='photoavatar[]']").each(function(i, el){
                  photoAvatarArr.push( $(el).val() );
                  });
                  
                  
                  var roleid= [];
                  oTable.$("input[name='role[]']").each(function(i, el){
                    if ($(el).attr("checked")) {
        // checked
                      roleid.push( 1 );
                 }else
                 {
                    roleid.push( 0 );  
                 }
                  
                  });
                  
                  
                  var tag= [];
                  $("input[name='tag[]']").each(function(i, el){
                   tag.push( $(el).val() );
                  });
                  
                  var tagid= [];
                  $("input[name='tagid[]']").each(function(i, el){
                  tagid.push( $(el).val() );
                  });
                  
                  if(usernameArr.length==0)
                  {
                     alert("กรอก อย่างน้อย 1 Username");
                       
                  }else if(emailArr.length==0)
                  {
                     alert("กรอก อย่างน้อย 1 Email");
                       
                  }else if(category.length==0)
                  {
                     alert("กรอก อย่างน้อย 1 Category");
                       
                  }else if(tag.length==0)
                  {
                     alert("กรอก อย่างน้อย 1 Tag");
                       
                  }else
                  {
                    if(confirm("ต้องการบันทึกข้อมูลใช่หรือไม่"))
                    {
                    
                      postCORS(serviceinfo+'saveshelf',{
                      photo:$("#filebirdsavename-0").val(),
                      title:$("#title").val(),
                      shelfid:$("#shelfid").val(),
                      itune:$("#itune").val(),
                      schema:$("#schema").val(),
                      
                      
                      username:usernameArr,
                      firstname:firstnameArr,
                      email:emailArr,
                      password:password,
                      userid:userid,
                      deleteuserid:deleterowuserid,
                      
                      tagid:tagid,
                      roleid:roleid,
                      tag:tag,
                      deletetagid:deleterowtagid,
                      photoavatar:photoAvatarArr,
                      
                      passpem:$("#passpem").val(),
                      pemfile:$("#file2birdsavename-0").val(),
                      

                      
                      
                      category:category,
                      catid:catid,
                      deletecatid:deleterowcatid,
                      
                      descritpion:$("#descritpion").val()
                      
                      
                      } ,function(data) {
                       
                      // return false;
                      if(data.error)
                      {
                      
                      alert(data.error);
                      }else
                      {
                    alert('บันทึกเรียบร้อยแล้ว');
                    location.href=baseinfo+"/editshelf?shelfid=1";  
                      
                      }
                      
                      
                      });
                    
                    }
                  
                  
                  }
    
    }

}